package com.dsa.linear;


/**
 * 平均复杂度 & 均摊复杂度 & 复杂度震荡
  * @param <E>
 */
public class Array<E> {

    private static final int DEFAULT_CAPACITY = 10;

    private E[] elements;
    private int size;


    public Array() {
        this(DEFAULT_CAPACITY);
    }

    public Array(int capacity) {
        elements = (E[]) new Object[capacity];
        size = 0;
    }

    public void addFirst(E element) {
        add(0, element);
    }

    public void add(int index, E element) {
        checkForAdd(index);
        if (size == capacity())
            resize( size * 2);

        for (int i = size; i > index; i--) {
            elements[i] = elements[i - 1];
        }
        elements[index] = element;
        size++;
    }

    public void addLast(E element) {
        add(size, element);
    }

    public E getFirst() {
        return get(0);
    }

    public E get(int index) {
        checkForUpdate(index);
        return elements[index];
    }

    public E getLast() {
        return get(size - 1);
    }

    public int find(E element) {
        for (int i = 0; i < size; i++)
            if (elements[i].equals(element))
                return i;
        return -1;
    }

    public boolean contains(E element) {
        return find(element) != -1;
    }

    public E set(int index, E element) {
        checkForUpdate(index);
        E oldValue = elements[index];
        elements[index] = element;
        return oldValue;
    }

    public E removeFirst() {
        return remove(0);
    }

    public E remove(int index) {
        checkForUpdate(index);
        E oldValue = elements[index];
        for (int i = index + 1; i < size ; i++) {
            elements[i - 1] = elements[ i ];
        }
        size--;
        elements[size] = null; // literal objects

        // 防止复杂度震荡 - eager 策略
        if (size == capacity() / 4 && capacity()/2 != 0)
            resize(capacity() / 2);

        return oldValue;
    }

    public E removeLast() {
        return remove(size - 1);
    }

    public void removeElements(E element) {
        int index = find(element);
        if (index != -1) {
            remove(index);
        }
    }

    public int size() {
        return size;
    }

    public int capacity() {
        return elements.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("Array(size = %d, capacity = %d)\n", size, capacity()));
        sb.append("  [");
        for (int i = 0; i < size; i++) {
            sb.append(elements[i]);
            if (i != size - 1) {
                sb.append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    private void checkForUpdate(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException(String.format("index out of range [%d, %d)", 0, size));
    }

    private void checkForAdd(int index) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException(String.format("index out of range [%d, %d]", 0, size));
    }

    // 均摊
    private void resize(int newCapacity) {
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
    }

}
